﻿using Barcode_Project.Entity;
using Barcode_Project.Local;
using Barcode_Project.Network;
using DevExpress.Utils.About;
using DevExpress.XtraEditors;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.WebRequestMethods;

namespace Barcode_Project.ReBar
{
    public partial class RePrint : DevExpress.XtraEditors.XtraForm
    {
        Dictionary<string, string> datas;
        HttpAgreement http;

        public RePrint(Dictionary<string, string> dic)
        {
            InitializeComponent();

            http = new HttpAgreement();

            datas = dic;

            new ComputerTask().LoadPrinter(select_printer);
            new ComputerTask().LoadTemplate(select_template);

            CommonMethods.SetFormValue(this.group_data.Controls, datas, "input_");

            GetGYPrintData();

        }

        string gyPrintData = "";
        // 获取计划的材质自定义字段
        /// <summary>
        /// 获取关联工序工艺打印字段值
        /// </summary>
        private void GetGYPrintData()
        {

            var queryDic = new Dictionary<string, string>();
            queryDic["plCode"] = datas["planCode"];
            queryDic["gxCode"] = datas["gxCode"];
            string url = CommonMethods.APIUrl() + "GetPrintGYData";
            var info = http.HttpPost(url, JsonConvert.SerializeObject(queryDic));
            if (string.IsNullOrWhiteSpace(info))
            {
                XtraMessageBox.Show("接口返回信息为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            var obj = JsonConvert.DeserializeObject<JObject>(info);

            if (bool.Parse(obj["result"].ToString()))
            {
                gyPrintData = obj["data"].ToString();
            }
        }


        private void button_reprint_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var barCode = datas["barCode"];
            var dt = new RemoteRequest().GetReBarInfo(barCode);

            if (!string.IsNullOrWhiteSpace(gyPrintData))
            {
                var json = JsonConvert.DeserializeObject<JObject>(gyPrintData);
                foreach (var j in json)
                {
                    if (!dt.Columns.Contains(j.Key.ToString()))
                    {
                        dt.Columns.Add(j.Key.ToString());
                    }
                }
                foreach (DataRow r in dt.Rows)
                {
                    r.BeginEdit();
                    foreach (var j in json)
                    {
                        r[j.Key.ToString()] = j.Value.ToString();
                    }
                    r.EndEdit();
                }
            }


            var printerName = ((ItemSelector)select_printer.GetSelectedDataRow()).code;
            var templatePath = ((ItemSelector)select_template.GetSelectedDataRow()).code;//模板路径

            new ComputerTask().StartPrintingTask(dt, printerName, templatePath);
        }

        private void button_exit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Application.Exit();
        }

        private void btn_update_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var r = XtraMessageBox.Show("即将关闭程序进行升级，是否继续？", "提示", MessageBoxButtons.OKCancel);
            if (r == DialogResult.OK)
            {
                new RemoteRequest().UpdateCmdProgram();
            }
        }
    }
}